//
//  GameDataCollection.m
//  ShovelBattle
//
//  Created by Eleve on 13/03/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GameDataController.h"

@implementation GameDataController

@synthesize mapDataDictionary;
@synthesize treasureMenuDictionary;

@synthesize mapSquare;
@synthesize allTreasuresDiscovered;
@synthesize treasureMenuTouchable;
@synthesize currentTreasureButton;

- (id)init 
{
    self = [super init];
    if (self) {
        [self initDatas];
    }
    return self;
}

- (void)dealloc
{
    [currentTreasureButton release];
    [super dealloc];
}

- (void)initDatas
{
    NSLog(@"init gameDataController");
    mapDataDictionary      = [[NSMutableDictionary alloc] init];
    treasureMenuDictionary  = [[NSMutableDictionary alloc] init];
}

- (void)addMapDataWithName:(int)name andCoordsA:(CGPoint)coordsA andCoordsB:(CGPoint)coordsB
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithInt:name], @"name",
                                        [NSNumber numberWithFloat:coordsA.x], @"xa",
                                        [NSNumber numberWithFloat:coordsA.y], @"ya",
                                        [NSNumber numberWithFloat:coordsB.x], @"xb",
                                        [NSNumber numberWithFloat:coordsB.y], @"yb",
                                        [NSNumber numberWithBool:NO], @"occupied", // if there is something on case
                                        [NSNumber numberWithBool:NO], @"visited", // if square has been dug
                                nil];
    
    [mapDataDictionary setObject:dict forKey:[NSNumber numberWithInt:name]];
}

- (void)addTreasureMenuDataWithName:(int)name
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithInt:name], @"name",
                                        [NSNumber numberWithBool:YES], @"touchable",
                                        [NSNumber numberWithBool:NO], @"selected",
                                 nil];
    
    [treasureMenuDictionary setObject:dict forKey:[NSNumber numberWithInt:name]];
}


#pragma mark - Setters

- (void)setTreasureButton:(id)name clickable:(BOOL)clickable
{
    NSMutableDictionary *dict = [treasureMenuDictionary objectForKey:name];
    [dict setObject:[NSNumber numberWithBool:clickable] forKey:@"touchable"];
}

- (void)setTreasureButton:(id)name selected:(BOOL)selected
{
    NSMutableDictionary *dict = [treasureMenuDictionary objectForKey:name];
    [dict setObject:[NSNumber numberWithBool:selected] forKey:@"selected"];
}

- (void)addTreasureOnMapBySquareId:(NSNumber *)squareId
{
    NSMutableDictionary *dict = [mapDataDictionary objectForKey:squareId];
    [dict setObject:[NSNumber numberWithBool:YES] forKey:@"occupied"];
    [dict setObject:[NSNumber numberWithBool:YES] forKey:@"treasure"];
    [dict setObject:[NSNumber numberWithBool:NO] forKey:@"discovered"];
}

- (void)setTreasureDiscoveredBySquareId:(NSNumber *)squareId
{
    NSLog(@"set treasure discovered");
    NSMutableDictionary *dict = [mapDataDictionary objectForKey:squareId];
    [dict setObject: [NSNumber numberWithBool:YES] forKey:@"discovered"];
}

- (void)setSquareDugBySquareId:(NSNumber *)squareId
{
    NSMutableDictionary *dict = [mapDataDictionary objectForKey:squareId];
    [dict setObject: [NSNumber numberWithBool:YES] forKey:@"visited"];
}

- (void)setCurrentTreasureButton:(NSNumber *)index
{
    currentTreasureButton = index;
}


#pragma mark - Getters

- (NSMutableDictionary *)compareDataWithCoords:(CGPoint)touchLocation
{
    float touchX = touchLocation.x;
    float touchY = touchLocation.y;
    
    mapSquare = nil;
    mapSquare = [[NSMutableDictionary alloc] init];
    
    [mapDataDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) 
    {
        NSDictionary *gameData = obj;
        float dictXa = [[gameData objectForKey:@"xa"] floatValue];
        float dictYa = [[gameData objectForKey:@"ya"] floatValue];
        float dictXb = [[gameData objectForKey:@"xb"] floatValue];
        float dictYb = [[gameData objectForKey:@"yb"] floatValue];
        
        if (touchX > dictXa && touchX <= dictXb && touchY > dictYa && touchY <= dictYb)
        {
            [mapSquare setObject:[gameData objectForKey:@"name"] forKey:@"name"];
            [mapSquare setObject:[gameData objectForKey:@"xa"] forKey:@"xa"];
            [mapSquare setObject:[gameData objectForKey:@"ya"] forKey:@"ya"];
            [mapSquare setObject:[gameData objectForKey:@"xb"] forKey:@"xb"];
            [mapSquare setObject:[gameData objectForKey:@"yb"] forKey:@"yb"];
            *stop = true;
        }
    }];
    
    return mapSquare;
}

- (BOOL)getAllTreasureDiscovered
{
    
    allTreasuresDiscovered = YES;
    [mapDataDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) 
     {
         NSDictionary *gameData = obj;
         BOOL occupied = [[gameData objectForKey:@"occupied"] boolValue];
         if (occupied == YES)
         {
             BOOL discovered = [[gameData objectForKey:@"discovered"] boolValue];
             if (discovered == NO)
             {
                 *stop = true;
                 allTreasuresDiscovered = NO;
             }
         }
     }];
    return allTreasuresDiscovered;
}

- (BOOL)isSquareOccupiedBySquareId:(NSNumber *)index
{
    NSMutableDictionary *dict = [mapDataDictionary objectForKey:index];
    BOOL isOccupied = [[dict objectForKey:@"occupied"] boolValue];
    return isOccupied;
}

- (BOOL)isSquareVisitedBySquareId:(NSNumber *)index
{
    NSMutableDictionary *dict = [mapDataDictionary objectForKey:index];
    BOOL isVisited = [[dict objectForKey:@"visited"] boolValue];
    return isVisited;
}

- (BOOL)isTreasureOnSquareBySquareId:(NSNumber *)index
{
    NSMutableDictionary *dict = [mapDataDictionary objectForKey:index];
    BOOL isTreasure = [[dict objectForKey:@"treasure"] boolValue];
    return isTreasure;
}

- (BOOL)getTreasureButtonTouchableById:(NSNumber *)index
{
    NSMutableDictionary *dict = [treasureMenuDictionary objectForKey:index];
    BOOL isTouchable = [[dict objectForKey:@"touchable"] boolValue];
    return isTouchable;
}

- (BOOL)getTreasureButtonSelectedById:(NSNumber *)index
{
    NSMutableDictionary *dict = [treasureMenuDictionary objectForKey:index];
    BOOL isSelected = [[dict objectForKey:@"touchable"] boolValue];
    return isSelected;
}

- (BOOL)isTreasureMenuTouchable
{
    treasureMenuTouchable = NO;
    [treasureMenuDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) 
    {
        NSNumber *touchable = [obj objectForKey:@"touchable"];
        if ([touchable boolValue] == YES)
        {
            *stop = true;
            treasureMenuTouchable = YES;
        }
    }];
    return treasureMenuTouchable;
}

- (NSNumber *)getCurrentTreasureButton
{
    return currentTreasureButton;
}

@end
